package com.idconfirmsystem.app;

import java.util.List;
import java.util.Properties;
import java.util.UUID;

import org.kymjs.kjframe.utils.StringUtils;

import com.idconfirmsystem.base.BaseApplication;
import com.idconfirmsystem.model.User;
import com.idconfirmsystem.model.UserMenu;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

/**
 * 全局应用程序类：用于保存和调用全局应用配置及访问网络数据
 * 
 */
public class AppContext extends BaseApplication {

	public static final int PAGE_SIZE = 20;// 默认分页大小

	private static AppContext instance;

	private String loginCode;// 登录的用户id
	private boolean login;// 是否登录
	private List<UserMenu> uMenuList;// 用户的菜单

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
	}

	/**
	 * 更新用户信息
	 * 
	 * @param user
	 */
	@SuppressWarnings("serial")
	public void updateUserInfo(final User user) {
		setProperties(new Properties() {
			{
				setProperty("user.name", user.getName());
				setProperty("user.code", user.getId());
				setProperty("user.type", user.getType() + "");
				setProperty("user.portrait", user.getPhoto_path());// 用户头像-文件名
			}
		});
	}

	/**
	 * 获得登录用户的信息
	 * 
	 * @return
	 */
	public User getLoginUser() {
		User user = new User();
		user.setId(getProperty("user.code"));
		user.setName(getProperty("user.name"));
		user.setType(getProperty("user.type"));
		return user;
	}

	/**
	 * 清除登录信息
	 */
	public void cleanLoginInfo() {
		this.loginCode = "0";
		this.login = false;
		removeProperty("user.code", "user.name", "user.portrait", "user.type", "user.isRememberMe", "user.orgcode",
				"user.orgname");
	}

	/**
	 * 获得当前app运行的AppContext
	 * 
	 * @return
	 */
	public static AppContext getInstance() {
		return instance;
	}

	public boolean containsProperty(String key) {
		Properties props = getProperties();
		return props.containsKey(key);
	}

	public void setProperties(Properties ps) {
		AppConfig.getAppConfig(this).set(ps);
	}

	public Properties getProperties() {
		return AppConfig.getAppConfig(this).get();
	}

	public void setProperty(String key, String value) {
		AppConfig.getAppConfig(this).set(key, value);
	}

	/**
	 * 获取cookie时传AppConfig.CONF_COOKIE
	 * 
	 * @param key
	 * @return
	 */
	public String getProperty(String key) {
		String res = AppConfig.getAppConfig(this).get(key);
		return res;
	}

	public void removeProperty(String... key) {
		AppConfig.getAppConfig(this).remove(key);
	}

	/**
	 * 获取App唯一标识
	 * 
	 * @return
	 */
	public String getAppId() {
		String uniqueID = getProperty(AppConfig.CONF_APP_UNIQUEID);
		if (StringUtils.isEmpty(uniqueID)) {
			uniqueID = UUID.randomUUID().toString();
			setProperty(AppConfig.CONF_APP_UNIQUEID, uniqueID);
		}
		return uniqueID;
	}

	/**
	 * 获取App安装包信息
	 * 
	 * @return
	 */
	public PackageInfo getPackageInfo() {
		PackageInfo info = null;
		try {
			info = getPackageManager().getPackageInfo(getPackageName(), 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace(System.err);
		}
		if (info == null)
			info = new PackageInfo();
		return info;
	}

	/**
	 * 清除保存的缓存
	 */
	public void cleanCookie() {
		removeProperty(AppConfig.CONF_COOKIE);
	}

	/**
	 * 判断当前版本是否兼容目标版本的方法
	 * 
	 * @param VersionCode
	 * @return
	 */
	public static boolean isMethodsCompat(int VersionCode) {
		int currentVersion = android.os.Build.VERSION.SDK_INT;
		return currentVersion >= VersionCode;
	}

	public List<UserMenu> getuMenuList() {
		return uMenuList;
	}

	public void setuMenuList(List<UserMenu> uMenuList) {
		this.uMenuList = uMenuList;
	}

}
